<template>
  <div class="box">
    <p class="hh">
        我是登录页面
    </p>
    <br />

    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-search"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="form.password" placeholder="请输入用户密码" prefix-icon="el-icon-search"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width:90%; margin-left: 5%;margin-top:20px" @click="submitForm('form')">登录</el-button>
      </el-form-item>
      
    </el-form>
  </div>
</template>

<script>
import {checkgohh} from '../api/index.js' //导入封装的方法

export default {
  name:'hh',
  components:{
  },
  data(){
      return{
        form: {
          username: '',
          password:'' 
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ]
        }
      }
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
            // console.log(this.form)
            checkgohh(this.form).then(res=>{
              console.log(res)
              if(res.meta.status == 200){//根据后端返回的状态码判断是否登录成功
                  localStorage.setItem('myToken',res.data.token)//登录成功之后拿到token值，然后把这个值放到本地缓存里面
              }else{
                this.$message({
                  showClose: true,
                  message:res.meta.msg,
                  type: 'error'
                });
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    }
  }

  
</script>

<style scope>
.box{
  background-color: aqua;
  margin-top: 0px;
  height:700px;
}
.hh{
  text-align: center;
  font-size: 30px;
  padding-top: 20px;
  font-weight: 600;
}
.el-input{
  width: 90%;
  margin-left: 5%;
  margin-top: 20px;
}
.el-form-item__error{
  margin-left: 5%;
  margin-top: 5px;
}
.el-form{
  background-color:white;
  width: 90%;
  height: 240px;
  margin-left: 5%;
}

</style>
